/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package menus;

import com.sun.lwuit.Command;
import com.sun.lwuit.Form;
import com.sun.lwuit.Image;
import com.sun.lwuit.Label;
import com.sun.lwuit.TextArea;
import java.io.IOException;
import java.io.InputStream;
import util.Menu;

/**
 *
 * @author nialldeasy
 */
public class Reader extends Menu {

    public boolean succesful = false;
    String text = null;
    Label image = null;

    public Reader(String name) {
        Name = name;
        try {
            image = new Label(Image.createImage("/images/" + Name + ".jpg"));
            if (image != null) {
                succesful = true;
            }
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        try {

            text = readFile(Name);

            if (text != null) {
                succesful = true;
            }

        } catch (Exception ex) {
            ex.printStackTrace();
        }

    }

    public void recycleBin() {
    text = null;
    image = null;
    }

    protected void execute(Form f) {
        succesful = false;
        this.setBackround("ice.jpg");
        this.setLayout(4);
        if (image != null) {
            f.addComponent(image);
        }
        if (text != null) {
            addText(text, f);
        }

        f.addCommand(new Command("Visit On Map"));
        //.....................add Image for text file if available......//
       // this.setBackround("paper.jpg");






    }

    public void addText(String text, Form f) {
        TextArea t = new TextArea(text);
        t.setEditable(false);
        f.addComponent(t);
    }

    public String readFile(String name) {
        InputStream is = getClass().getResourceAsStream("/Landmarks/" + Name);
        StringBuffer sb = new StringBuffer();
        try {
            int chars, i = 0;
            while ((chars = is.read()) != -1) {
                sb.append((char) chars);
            }
            return sb.toString();
        } catch (Exception e) {
        }
        return null;
    }

    public void update() {
    }
}
